﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL.ComplexClasses;
using BLL;
using System.IO;
using System.Configuration;
using ithub.Properties;


namespace ithub.Controllers
{
    public class UsersController : Controller
    {
        //
        // GET: /Users/

        User objUser = null;


        public ActionResult Index()
        {
            return View();
        }

        
        public JsonResult LoadUsersList(App_Utils.jQueryDataTableParamModel param, string name)
        {
            if (Session[App_Utils.clsConstants.SESSION_USER] != null)
            {
                int totalRecord;
                objUser = Session[App_Utils.clsConstants.SESSION_USER] as User;

                List<User> lstUsers = BL_Users.GetClientUsers(objUser.client_id, param, out totalRecord);
                return Json(new
                {
                    sEcho = param.sEcho,
                    iTotalRecords = totalRecord,
                    iTotalDisplayRecords = lstUsers.Count,
                    aaData = lstUsers
                }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new
                {
                    sEcho = param.sEcho,
                    iTotalRecords = 0,
                    iTotalDisplayRecords = 0,
                    aaData = new List<string[]>(),
                    TotalRecordCount = 0
                }, JsonRequestBehavior.AllowGet);
            }
            
        }


        public ActionResult AddUser()
        {
            objUser = new User();
            return View(objUser);
        }

        public JsonResult Save(User objUser)
        {
            if (Session[App_Utils.clsConstants.SESSION_USER] != null)
            {
                User CurrentUser = Session[App_Utils.clsConstants.SESSION_USER] as User;

                objUser.client_id = CurrentUser.client_id;
                objUser = BL_Users.SaveRecord(objUser);

                BL_Permission.SaveRecord(new Permission()
                {
                    client_id = objUser.client_id,
                    user_id = objUser.user_id,
                    external_chat = false,
                    internal_chat = false,
                    is_superadmin = false,
                    is_admin = false
                });

                if (objUser == null)
                {
                    return Json(new { Result = App_Utils.clsConstants.ERROR }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { Result = App_Utils.clsConstants.OK, Data = objUser }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return Json(new { Result = App_Utils.clsConstants.ERROR }, JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult EditCurrentUser()
        {
            
            ithub.Models.LoginUser CurrentUser = Session[App_Utils.clsConstants.SESSION_USER_ALL] as ithub.Models.LoginUser;
            ViewBag.heading1 = "Edit user";
            ViewBag.heading2 = CurrentUser.myObject.first_name;
            return View("EditCurrentUser", CurrentUser);
        }

        [HttpPost]
        public ActionResult UploadImage(Image img)
        {
            string virtualpath = Resources.UserImage.Replace("~", "..");

            if (Request.Files != null)
            {
                foreach (string uploadedFileName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[uploadedFileName];
                    if (file != null && file.ContentLength > 0)
                    {
                        string fileExtension = Path.GetExtension(file.FileName);
                        string fileName = Guid.NewGuid().ToString() + fileExtension;
                        string dir = Server.MapPath(Resources.UserImage);
                        string imagePath = Path.Combine(dir, fileName);
                        virtualpath = virtualpath + fileName;
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        file.SaveAs(imagePath);

                        ithub.Models.LoginUser CurrentUser = Session[App_Utils.clsConstants.SESSION_USER_ALL] as ithub.Models.LoginUser;

                       
                        img.client_id = CurrentUser.myObject.client_id;
                        img.is_default = true;
                        img.is_visible = true;
                        img.path = virtualpath;
                        img.create_by = CurrentUser.myObject.user_id;
                        img.create_date = DateTime.Now;

                        BL_Images.SaveRecord(img);
                    }
                }
            }

            return Json(new { Result = App_Utils.clsConstants.OK, Path = virtualpath });
        }


        public ActionResult AddEditUser(int UserId = 0)
        {
            if (UserId == 0)
            {
                objUser = new User();
            }
            else
            {
                objUser = new User();
            }

            return View(objUser);
        }
    }
}
